package org.example.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Aspect
@Component
public class ProjectAdvice {
    // 切点
    @Pointcut("execution(* org.example.service.*Service.*(..))")
    public void pt(){}

    // 定义环绕通知
    @Around("pt()")
    public void runSpeed(ProceedingJoinPoint pjp) {
        // 获取执行方法的签名对象
        Signature signature = pjp.getSignature();
        String className = signature.getDeclaringTypeName();
        String methodName = signature.getName();

        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            try {
                pjp.proceed();
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(className + "." + methodName + "()方法执行耗时：" + (end - start) + "毫秒");
    }
}
